<?php

/**
 * @author Kevin
 * @version 1.0
 * @created 12-d�c.-2011 19:51:40
 */
class Event extends Publication implements I_Commentable
{

	/**
	 * Déclarations des données
	 */
	var $league = NULL;
	var $team1 = NULL;
	var $team2 = NULL;


	/**
	 * Constructeur
	 * 
	 * @param pId
	 */
	function Event($pId)
	{
		global $wpdb;
		
		// Instanciation de parent
		parent::Publication("EVENT", $pId);
		
		$_row = 
		$wpdb->get_row(
			$wpdb->prepare(
				"SELECT * 
				FROM events 
				WHERE id = %d", 
			
				$pId
			)
		);
		
		if($_row->id > 0 ){
			$this->league = $_row->fk_league;
			$this->team1 = $_row->team1;
			$this->team2 = $_row->team2;
			
			$this->exist = true;
		}		
		
	}

	/**
	 * ====================== Création ======================
	 */
	function create()
	{
		global $wpdb;
			
		$queryTable = 'events';
		$queryData = array(
					'id' => $this->id, 
					'fk_league' => $this->league,
					'team1' => $this->team1,
					'team2' => $this->team2
		);
		$queryDataFormat = array('%d','%d','%s','%s');
		
		$wpdb->insert( $queryTable, $queryData, $queryDataFormat);
				
		parent::setId($this->getId());
		parent::create();
	}

	function delete()
	{
	}

	function getBets()
	{
		global $wpdb;
		
		return
		$wpdb->get_results(
			$wpdb->prepare(
				// requete
				"SELECT id FROM bets WHERE fk_event = %d ORDER BY fk_bettype, tip",
				// parametres
				$this->getId()
			)
		);
	}

	function getLeague(){
		return new League($this->league);
	}
	
	function setLeague($pLeague){
		$this->league = $pLeague;
	}
	
	function getTeam($pLocation){
		switch ($pLocation){
			case 'home' :
				return $this->team1;
				break;
			case 'away' :
				return $this->team2;
				break;
			default :
				return "undefined";
		}
	}
	
	function setTeam($pLocation, $pName){
		switch ($pLocation){
			case 'home' :
				$this->team1 = $pName;
				break;
			case 'away' :
				$this->team2 = $pName;
				break;
			default :
				return "error location";
		}
	}
	
	function getName(){
		return $this->team1." - ".$this->team2;
	}

	function save()
	{
		global $wpdb;
			
		$queryTable = 'events';
		$queryData = array(
					'fk_league' => $this->league,
					'team1' => $this->team1,
					'team2' => $this->team2,
		);
		$queryDataFormat = array('%d','%s','%s','%s','%s');
		$queryCondition = array("id" => $this->id);
		$queryConditionFormat = array('%d');
		
		$wpdb->update( $queryTable, $queryData, $queryCondition, $queryDataFormat, $queryConditionFormat);
		
		parent::save();
	}

	
	function toHTML()
	{
	}

	
	/**
	*
	* @param comment
	*/
	function addComment($comment)
	{		 		
		$newComment = new Comment();
		$newComment->setReference($this->getPublicationId());
		$newComment->setComment($comment);

		$newComment->create();
	}
	
	function getLastComment(){
		global $wpdb;
		
		$commentId =
		$wpdb->get_var(
			$wpdb->prepare(
				// request
				"SELECT id FROM comments WHERE fk_publicationid = %d ORDER BY id DESC LIMIT 1",
				// params
				$this->getPublicationId()
			)
		);

		if($commentId > 0){
			return new Comment($commentId);
		}
		
		return null;
	}
	
	function getComments()
	{
		global $wpdb;
		
		return
		$wpdb->get_results(
			$wpdb->prepare(
				// request
				"SELECT id FROM comments WHERE fk_publicationid = ".$this->getPublicationId()." ORDER BY id",
				// params
				$this->getPublicationId()
			)
		);
		
	}

}
?>